<?php
declare (strict_types = 1);

namespace app\homl\controller;

use app\BaseController;
use app\homl\model\User;
use app\homl\services\RegisterServices;
use think\facade\Cache;
use think\Request;

class Login extends BaseController
{
    public function login(){
        return view('register');
    }
    public function doLogin(Request $request){
        return view('login');
    }

    public function register(Request $request){
        $params=$request->post();
        dd($params);
        try {
            $code=mt_rand(1000,9999);
//            echo $code;
//            $code='2023年马上来临，国足祝您开心快乐,时代在飞速发展,不要太累哦,我们虽会经历一个又一个的困难，身边的人也都在互不联系中渐行渐远渐,但愿您慢下脚步,把生活节奏放慢,，但成长就是这样,结交新友时,也多留意故人';
            $content='测试码:'.$code;



            //缓存的验证码
            $smsCode='sms_code'.$params['phone'];
            dd($smsCode);

            //发送频率限制
            $smsSendTime='sms_send_time'.$params['phone'];
            $sendTime=Cache::get($smsSendTime);
            if($sendTime){
                if(time()-$sendTime<60){
                    $endTime=60-(time()-$sendTime);
                    abort(102,'你的发送频率过于频繁，请'.$endTime.'稍后再试');
                }
            }

            //发送次数限制
            $smsSendDay='sms_day_'.$params['phone'] . '_' . date('ymd');
            dd($smsSendDay);
            $sendDayTime=Cache::get($smsSendDay);
            if($sendDayTime && $sendDayTime>5){
                abort(103,'验证码超出一天的发送次数');
            }


            $res=RegisterServices::register($params['phone'],$content,true);

            if($res['status']==0){
                //短信缓存有效期
                Cache::set($smsCode,$code,600);
                //第一次发送时间
                Cache::set($smsSendTime,time(),60);
                //自增
                Cache::inc($smsSendDay);
                return $res;
            }else{
                abort(101,$res['msg']);
            }
            return $res;
        }catch (\Exception $e){
            return ['status'=>0,'msg'=>$e->getMessage()];
        }

    }

    public function sendSms(Request $request){
        if($request->post()){
            $params=$request->post();
            try {

                $this->validate($params,[
                   'phone'=>'require|unique:user',
                   'code'=>'require',
                   'password'=>'require',
                   'repassword'=>'require|confirm:password',
                ],[
                    'repassword.confirm'=>'俩次密码不一致',
                    'code.require'=>'验证码不能为空',
                    'phone.unique'=>'手机号已经注册，请不要重复注册'
                ]);
                //存的验证码
                $smsCode='sms_code'.$params['phone'];
                //取出除存的验证码
                $code=Cache::get($smsCode);

                //判断缓存验证码是否存在
                if(!$code){
                    abort(104,'请输入验证码');
                }

                //验证码缓存中取出的验证码是否与输入的验证码相同
                if($code != $params['code']){
                    abort(105,'验证码输入错误');
                }

                $data=[
                    'phone'=>$params['phone'],
                    'username'=>$params['phone'],
                    'password'=>md5($params['password']),
                ];
                $user=User::create($data);
                if($user->id){
                    return ['status'=>1,'msg'=>'注册成功，请重新登录'];
                }else{
                    return ['status'=>0,'msg'=>'注册失败'];
                }
            }catch (\Exception $e){
                return ['status'=>0,'msg'=>$e->getMessage()];
            }
        }
    }
    public function editLogin(Request $request){
        if($request->isPost()){
            $data=$request->post();
                $username=User::where('username',$data['username'])->find();
                if(!$username){
                    abort(106,'用户名错误');
                }else{
                    $password=md5($data['password']);
                    if($password != $username['password']){
                        abort(107,'密码错误');
                    }else{
                        return redirect(url('index/index')->build());
                    }
                }
        }
    }
}
